from tkinter import Tk
from database.table import tablee
from database.table.tablee import Supplier
import tkinter as tk

def AdmendSupplier():
    admend_supplier_screen = Tk()
    admend_supplier_screen.title('修改供应商信息')
    screen_width, screen_height = admend_supplier_screen.maxsize()
    # 窗口的宽和高
    width = 605
    height = 280
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    admend_supplier_screen.geometry(centre)

    label = tk.Label(admend_supplier_screen, text='请输入要修改的供应商编号，填写要修改的信息：', font=('楷体', 14))
    label.place(x=20, y=15)

    # 编号标签和文本框
    user_num_label = tk.Label(admend_supplier_screen, text='编号', font=('楷体', 14))
    user_num_label.place(x=20, y=60)
    user_num_text = tk.StringVar()
    user_num_entry = tk.Entry(admend_supplier_screen, textvariable=user_num_text,
                              width=15)
    user_num_entry.place(x=70, y=60)
    # 名称标签和文本框
    user_allname_label = tk.Label(admend_supplier_screen, text='名称', font=('楷体', 14))
    user_allname_label.place(x=210, y=60)
    user_allname_text = tk.StringVar()
    user_allname_entry = tk.Entry(admend_supplier_screen, textvariable=user_allname_text,
                                  width=19)
    user_allname_entry.place(x=260, y=60)
    # 简称标签和文本框
    user_name_label = tk.Label(admend_supplier_screen, text='简称', font=('楷体', 14))
    user_name_label.place(x=415, y=60)
    user_name_text = tk.StringVar()
    user_name_entry = tk.Entry(admend_supplier_screen, textvariable=user_name_text,
                               width=17)
    user_name_entry.place(x=470, y=60)
    # 地址标签和文本框
    user_address_label = tk.Label(admend_supplier_screen, text='地址', font=('楷体', 14))
    user_address_label.place(x=415, y=110)
    user_address_text = tk.StringVar()
    user_address_entry = tk.Entry(admend_supplier_screen, textvariable=user_address_text,
                                  width=17)
    user_address_entry.place(x=470, y=110)
    # 电话标签和文本框
    user_phone_label = tk.Label(admend_supplier_screen, text='电话', font=('楷体', 14))
    user_phone_label.place(x=20, y=110)
    user_phone_text = tk.StringVar()
    user_phone_entry = tk.Entry(admend_supplier_screen, textvariable=user_phone_text,
                                width=15)
    user_phone_entry.place(x=70, y=110)
    # 邮箱标签和文本框
    user_email_label = tk.Label(admend_supplier_screen, text='邮箱', font=('楷体', 14))
    user_email_label.place(x=210, y=110)
    user_email_text = tk.StringVar()
    user_email_entry = tk.Entry(admend_supplier_screen, textvariable=user_email_text,
                                width=19)
    user_email_entry.place(x=260, y=110)
    # 联系人标签和文本框
    user_contact_label = tk.Label(admend_supplier_screen, text='联系人', font=('楷体', 12))
    user_contact_label.place(x=20, y=160)
    user_contact_text = tk.StringVar()
    user_contact_entry = tk.Entry(admend_supplier_screen, textvariable=user_contact_text,
                                  width=15)
    user_contact_entry.place(x=70, y=160)
    # 联系人电话标签和文本框
    user_conphone_label = tk.Label(admend_supplier_screen, text='联系人电话', font=('楷体', 12))
    user_conphone_label.place(x=195, y=160)
    user_conphone_text = tk.StringVar()
    user_conphone_entry = tk.Entry(admend_supplier_screen, textvariable=user_conphone_text,
                                   width=15)
    user_conphone_entry.place(x=285, y=160)
    # 备注标签和文本框
    user_remarks_label = tk.Label(admend_supplier_screen, text='备注', font=('楷体', 14))
    user_remarks_label.place(x=415, y=160)
    user_remarks_text = tk.StringVar()
    user_remarks_entry = tk.Entry(admend_supplier_screen, textvariable=user_remarks_text,
                                  width=17)
    user_remarks_entry.place(x=470, y=160)

    def confirm():
        id = str(user_num_entry.get())
        suppliee = tablee.session.query(Supplier).filter(Supplier.Sno == id).all()
        data = Supplier.to_json(suppliee)
        length = len(data)

        if length == 1: #修改编号存在

            name = str(user_name_entry.get())
            allname = str(user_allname_entry.get())
            address = str(user_address_entry.get())
            phone = str(user_phone_entry.get())
            email = str(user_email_entry.get())
            contact = str(user_contact_entry.get())
            conphone = str(user_conphone_entry.get())
            remarks = str(user_remarks_entry.get())
            if allname != '':
                supplierr = Supplier (Sno=id,Sallname=allname)
                tablee.session.merge(supplierr)
                tablee.session.commit()
            if name != '':
                supplierr = Supplier(Sno=id, Sname=name)
                tablee.session.merge(supplierr)
                tablee.session.commit()
            if address != '':
                supplierr = Supplier (Sno=id,Saddress=address)
                tablee.session.merge(supplierr)
                tablee.session.commit()
            if phone != '':
                supplierr = Supplier (Sno=id,Sphone=phone)
                tablee.session.merge(supplierr)
                tablee.session.commit()
            if email != '':
                supplierr = Supplier (Sno=id,Semail=email)
                tablee.session.merge(supplierr)
                tablee.session.commit()
            if contact != '':
                supplierr = Supplier (Sno=id,Scontact=contact)
                tablee.session.merge(supplierr)
                tablee.session.commit()
            if conphone != '':
                supplierr = Supplier (Sno=id,Sconphone=conphone)
                tablee.session.merge(supplierr)
                tablee.session.commit()
            if remarks != '':
                supplierr = Supplier(Sno=id, Sremarks=remarks)
                tablee.session.merge(supplierr)
                tablee.session.commit()

            tablee.session.close()

            admend = tk.Toplevel(admend_supplier_screen)
            admend.title("Success")
            screen_width, screen_height = admend.maxsize()
            # 窗口的宽和高
            width = 240
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            admend.geometry(centre)
            label = tk.Label(admend, text='修改成功！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                admend.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(admend, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=65, y=60)

        elif id == '':  # 编号为空
            admend = tk.Toplevel(admend_supplier_screen)
            admend.title("Error")
            screen_width, screen_height = admend.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            admend.geometry(centre)
            label = tk.Label(admend, text='编号不能为空，请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                admend.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(admend, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

        else:  # 编号不存在
            admend = tk.Toplevel(admend_supplier_screen)
            admend.title("Error")
            screen_width, screen_height = admend.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            admend.geometry(centre)
            label = tk.Label(admend, text='此编号不存在,请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                admend.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(admend, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

    # 返回按钮响应事件
    def back():
        admend_supplier_screen.destroy()

    # 确定按钮
    user_confirm_button = tk.Button(admend_supplier_screen, text='确定', font=('楷体', 14),
                                    width=10, command=confirm)
    user_confirm_button.place(x=130, y=220)
    # 返回按钮
    user_back_button = tk.Button(admend_supplier_screen, text='返回', font=('楷体', 14),
                                 width=10, command=back)
    user_back_button.place(x=370, y=220)

    admend_supplier_screen.mainloop()


if __name__ == '__main__':
    AdmendSupplier()